﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.WebPages;

namespace W2R.Support
{
    /// <summary>
    /// Html extension for pivot rendering.
    /// </summary>
    public static class PivotHelper
    {
        /// <summary>
        /// Creates a pivot HTML control with the url for each tab
        /// </summary>
        /// <param name="html">html extension for</param>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        /// <param name="initialPage">The initial page.</param>
        /// <param name="urls">urls for each tab in the pivot control, key: TabName value: url</param>
        /// <returns></returns>
        public static HtmlString Pivot(this HtmlHelper html, object htmlAttributes, IHtmlString initialPage, params KeyValuePair<string, string>[] urls)
        {
            TagBuilder metroPivotBuilder = new TagBuilder("section");
            metroPivotBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            metroPivotBuilder.AddCssClass("metro-pivot");

            bool first = true;
            foreach (KeyValuePair<string, string> url in urls)
            {
                TagBuilder pivotItemBuilder = new TagBuilder("div");
                pivotItemBuilder.AddCssClass("pivot-item");
                pivotItemBuilder.Attributes.Add("url", url.Value);

                TagBuilder h3ItemBuilder = new TagBuilder("h3");
                h3ItemBuilder.SetInnerText(url.Key);
                pivotItemBuilder.InnerHtml = h3ItemBuilder.ToString();

                if (first && initialPage != null)
                {
                    pivotItemBuilder.InnerHtml += initialPage.ToHtmlString();
                    first = false;
                }

                metroPivotBuilder.InnerHtml += pivotItemBuilder.ToString();
            }
            return new MvcHtmlString(metroPivotBuilder.ToString());
        }
    }
}